global n = 2
global rhs1 diff_ln_IND 
global rhs2 diff_wIPT_IND_CHN
global ivs1 diff_ln_IVS 
global ivs2 diff_wIPT_LMI_CHN_EXCL_IND
global ctrl 


use "$working/$datafiles", clear

xtset  co_code1 year, yearly
destring(nic_08_2dig), g(nic2)

gen diff_ln_IND = ln_IND - L.ln_IND
gen diff_ln_IVS = ln_IVS - L.ln_IVS
gen diff_tfpr = tfpr - L.tfpr

foreach var in wIPT_IND_CHN wIPT_LMI_CHN_EXCL_IND rwIPT_IND_CHN rwIPT_LMI_CHN_EXCL_IND {
	generate ln_`var' = ln(`var')
	generate diff_`var' = ln_`var' - L.ln_`var'
}

foreach v in diff_tfpr diff_ln_IND diff_ln_IVS {
	drop if missing(`v')
}

** To get the final sample for all regs:
	ivreghdfe drop_term ($rhs1 $rhs2 = $ivs1 $ivs2 ) $ctrl , a(nic2#year) first cluster(nic_08_4dig)
			generate sample0 = e(sample)
			keep if sample0 == 1


foreach variable in dif_d_bar dif_a_bar dif_cov_stay dif_var_adj adding_term drop_term {
	drop if `variable'==.
}
generate test_me = dif_d_bar + dif_a_bar + dif_cov_stay + dif_var_adj + adding_term + drop_term

replace diff_tfpr = full_dif_TFPR // This is to make adjustments for changing production function parameters; this holds parameters fixed at lagged valued

generate diff = (test_me - diff_tfpr)/diff_tfpr

sum diff, detail

quietly {
	
	foreach var in diff_tfpr dif_d_bar dif_a_bar dif_cov_stay dif_var_adj adding_term drop_term {

* Baseline IV 		

		
	ivreghdfe `var' ($rhs1 $rhs2 = $ivs1 $ivs2 ) $ctrl  , a(year nic2) first cluster(nic_08_4dig)
				local b`n'_yn = _b[diff_ln_IND]
				local se_b`n'_yn = _se[diff_ln_IND]
				cap local I_b2_yn = _b[diff_wIPT_IND_CHN]
				cap local se_I_b2_yn = _se[diff_wIPT_IND_CHN]
				cap local I_b3_yn = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_I_b3_yn = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local N`n'_yn = e(N)
				mat f = e(first)
				local F_b`n'_yn = round(f[8,1], .01)
				cap local F_I_b`n'_yn = round(f[8,2], .01)
				generate bIC_`var' = _b[diff_ln_IND]
				generate bMA_`var' = _b[diff_wIPT_IND_CHN]

	}
}
generate bIC_a_diff_tfpr=  bIC_dif_d_bar +  bIC_dif_a_bar +  bIC_dif_cov_stay +  bIC_dif_var_adj +  bIC_adding_term +  bIC_drop_term
generate bMA_a_diff_tfpr=  bMA_dif_d_bar +  bMA_dif_a_bar +  bMA_dif_cov_stay +  bMA_dif_var_adj +  bMA_adding_term +  bMA_drop_term

generate t1= bIC_a_diff_tfpr - bIC_diff_tfpr
sum t1
generate t2 = bMA_a_diff_tfpr - bMA_diff_tfpr
sum t2
